package com.test;

import java.util.HashSet;
import java.util.Set;

public class Subject {
    private final Set<Observer> observerSet = new HashSet<>();

    public void observe(Observer observer) {//添加观察者
        observerSet.add(observer);
    }

    public void modify() {
        System.out.println("Subject修改");
        observerSet.forEach(Observer::update);//修改后通知观察者
    }

    public static void main(String[] args) {
        Subject subject = new Subject();
        subject.observe(()-> System.out.println("观察者1号接收通知并执行"));
        subject.observe(()-> System.out.println("观察者2号接收通知并执行"));
        subject.modify();//修改动作
    }
}
